<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="basic-business"
                v-decorator="['mcode', validatorRules.mcode]"
                :trigger-change="true"
                org-fields="mid,mcode,mname,mexplain,sprc,char5g"
                dest-fields="mid,mcode,mname,sprc,explain,sn_field_type5g"
                code="wms_cpinfo"
                @callback="popupCallback"
              />
              <!-- <f-popup
						contextPath="basic-business"
						v-decorator="['mcode', validatorRules.mcode]"
						:trigger-change="true"
						org-fields="mid,mcode,mname,mexplain,sprc,char5g"
						dest-fields="mid,mcode,mname,sprc,explain,sn_field_type5g"
						code="mes_m5g"
              @callback="popupCallback"/>-->
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                disabled
                v-decorator="[ 'mname', validatorRules.mname]"
                placeholder="请输入产品名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="5G产品类型字段字符" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-select
                show-search
                placeholder="请选择5G产品类型字段字符"
                option-filter-prop="children"
                v-decorator="[ 'snFieldType5g', validatorRules.snFieldType5g]"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
              >
                <p-select-option :value="item.productFieldChar5g" v-for="(item, index) in mesBcTemplateList" :key="index">
                  {{item.productFieldChar5g}}
                </p-select-option>
              </p-select>  
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="基站" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'baseStation', validatorRules.baseStation]"
                placeholder="请输入基站"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'baseNo', validatorRules.baseNo]" placeholder="请输入编号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="5G产品序列号字段字符" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'snFieldChar5g', validatorRules.snFieldChar5g]"
                placeholder="请输入5G产品序列号字段字符"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";

export default {
  name: "MesBc5gSnModal",
  mixins: [SubmitOnchangMixin],
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        mcode: { rules: [{ required: true, message: "请输入产品编码!" }] },
        mname: { rules: [{ required: true, message: "请输入产品名称!" }] },
        snFieldType5g: {
          rules: [{ required: true, message: "请输入5G产品类型字段字符!" }],
        },
        baseStation: { rules: [{ required: true, message: "请输入基站!" }] },
        baseNo: { rules: [{ required: true, message: "请输入编号!" }] },
        snFieldChar5g: {
          rules: [{ required: true, message: "请输入5G产品序列号字段字符!" }],
        },
        remark: {},
      },
			mesBcTemplateList: [],			
      url: {
        add: "/mes-code/mesBc5gSn",
        edit: "/mes-code/mesBc5gSn",
				// mesBcTemplateList: '/mes-code/mesBcTemplate/all', // 标签信息列表
				mesBcTemplateList: '/mes-code/mesBc5g/list', // 标签信息列表
      },
    };
  },
  created() {
    this.getMesBcTemplateList()
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "timestamp",
            "companyId",
            "factoryId",
            "clientId",
            "substituteId",
            "flag",
            "mid",
            "mcode",
            "mname",
            "sprc",
            "mexplain",
            "snFieldType5g",
            "baseStation",
            "baseNo",
            "snFieldChar5g",
            "mesBcFieldId",
            "remark",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime"
          )
        );
      });
    },   
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);

          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "timestamp",
          "companyId",
          "factoryId",
          "clientId",
          "substituteId",
          "flag",
          "mid",
          "mcode",
          "mname",
          "sprc",
          "mexplain",
          "snFieldType5g",
          "baseStation",
          "baseNo",
          "snFieldChar5g",
          "mesBcFieldId",
          "remark",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime"
        )
      );
    },


		getMesBcTemplateList() {
      
			const parmas = {
				pageSize: 10000,
			}
			getAction(this.url.mesBcTemplateList, parmas).then( res => {
				if ( res.success) {
					this.mesBcTemplateList = res.data.records
				}
			} )
    },
      
		handleChange(value) {
			console.log(`selected ${value}`);
		},
		handleBlur() {
			console.log('blur');
		},
		handleFocus() {
			console.log('focus');
		},
		filterOption(input, option) {
			return (
				option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
			);
		},

  },
};
</script>
